package io.github.simplejy.miodemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import io.github.simplejy.miodemo.R;
import io.github.simplejy.miodemo.bean.UserApplicationData;

/**
 * 用户应用列表数据适配器
 */

public class UserApplicationAdapter extends BaseAdapter {

    private ArrayList<UserApplicationData> datas;
    private LayoutInflater mInflater;
    private boolean isShowApplicationName = true;

    public UserApplicationAdapter(Context context, ArrayList<UserApplicationData> datas) {
        mInflater = LayoutInflater.from(context);
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_select_application, null);
            holder = new ViewHolder();
            holder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_application_icon);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_application);
            holder.ivSelectIcon = (ImageView) convertView.findViewById(R.id.iv_select);

            convertView.setTag(R.id.holder, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.holder);
        }

        convertView.setTag(R.id.position, position);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取tag得到position
                int p = (int) v.getTag(R.id.position);
                boolean select = datas.get(p).getSelected();
                if (select) {
                    datas.get(p).setSelected(false);
                } else {
                    datas.get(p).setSelected(true);
                }
                notifyDataSetChanged();
            }
        });
        //holder.ivSelecIcon.setImageBitmap(datas.get(position).getApplicationIcon());

        if (isShowApplicationName) {
            holder.tvTitle.setText(datas.get(position).getApplicationTitle());
        } else {
            holder.tvTitle.setText(datas.get(position).getApplicationPackageName());
        }

        Boolean isSelect = datas.get(position).getSelected();
        if (isSelect) {
            holder.ivSelectIcon.setVisibility(View.VISIBLE);
        } else {
            holder.ivSelectIcon.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    public void switchDisplay() {
        if (isShowApplicationName) {
            isShowApplicationName = false;
        } else {
            isShowApplicationName = true;
        }
        notifyDataSetChanged();
    }

    private class ViewHolder {
        ImageView ivIcon;
        TextView tvTitle;
        ImageView ivSelectIcon;
    }
}
